package com.mars.proxy.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author King
 * @create 2020/8/26 16:19
 */
public class CglibProxy implements MethodInterceptor {


    public Object proxy(Object target) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }


    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("##########cglib before################");
        Object res = methodProxy.invokeSuper(o, objects);
        System.out.println("##########cglib after################");
        return res;
    }
}
